package com.tyt.travel.service.impl;

import com.alibaba.fastjson.JSON;
import com.tyt.travel.common.LocalUser;
import com.tyt.travel.common.util.IOUtil;
import com.tyt.travel.dto.AddEvaluationDTO;
import com.tyt.travel.dto.scenery.CreateOrUpdateSceneryDTO;
import com.tyt.travel.mapper.EvaluateMapper;
import com.tyt.travel.mapper.SceneryMapper;
import com.tyt.travel.model.EvaluateEntity;
import com.tyt.travel.model.SceneryEntity;
import com.tyt.travel.model.UserDO;
import com.tyt.travel.service.SceneryService;
import com.tyt.travel.service.UserService;
import com.tyt.travel.vo.SearchSceneryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author tianyun
 * @version 1.0
 * @date Created in 2021年02月10日 20:39
 * @since 1.0
 */
@Slf4j
@Service
public class SceneryServiceImpl implements SceneryService {

    @Autowired
    SceneryMapper sceneryMapper;
    @Autowired
    EvaluateMapper evaluateMapper;
    @Autowired
    IOUtil ioUtil;
    @Autowired
    UserService userService;

    @Override
    public List<SearchSceneryVO> searchScenery(String name) {
        List<SceneryEntity> list = sceneryMapper.searchScenery(name);
        List<SearchSceneryVO> res = new ArrayList<>();
        list.forEach(e->{
            SearchSceneryVO vo = new SearchSceneryVO();
            BeanUtils.copyProperties(e,vo);
            vo.setSceneryImg(JSON.parseArray(e.getSceneryImg(), String.class));
            res.add(vo);
        });
        return res;
    }

    /**
     * 添加景点
     *
     * @param validator
     */
    @Override
    public void addScenery(CreateOrUpdateSceneryDTO validator) {

        SceneryEntity entity = new SceneryEntity();
        entity.setDesc(validator.getDesc());
        entity.setSceneryName(validator.getSceneryName());

        getFilePath(entity, validator);

        sceneryMapper.insertSelective(entity);
    }

    /**
     * 景点详情
     *
     * @return
     */
    @Override
    public SceneryEntity sceneryDetail(Integer sceneryId) {
        return sceneryMapper.selectByPrimaryKey(sceneryId);
    }

    /**
     * 景点评价
     *
     * @param dto
     */
    @Override
    public void addEvaluation(AddEvaluationDTO dto) {
        EvaluateEntity entity = new EvaluateEntity();
        entity.setContent(dto.getContent());
        entity.setSceneryId(dto.getSceneryId());
        entity.setStarLevel(dto.getStarLevel());

        UserDO user = LocalUser.getLocalUser();
        entity.setUserName(user.getUsername());

        evaluateMapper.insertSelective(entity);
    }

    /**
     * 景点评价列表
     *
     * @param sceneryId
     * @return
     */
    @Override
    public List<EvaluateEntity> sceneryEvaluationList(Integer sceneryId) {
        return evaluateMapper.selectBySceneryId(sceneryId);
    }

    /**
     * 景点点击计数
     *
     * @param sceneryId
     * @return
     */
    @Override
    public void sceneryCount(Integer sceneryId) {
        SceneryEntity entity = sceneryMapper.selectByPrimaryKey(sceneryId);
        entity.setCount(entity.getCount()+1);
        sceneryMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public SceneryEntity getById(Integer id) {
        return sceneryMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(Integer id) {
        sceneryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 更新景区
     *
     * @param entity
     * @param validator
     */
    @Override
    public void updateScenery(SceneryEntity entity, CreateOrUpdateSceneryDTO validator) {
        entity.setSceneryName(validator.getSceneryName());
        entity.setDesc(validator.getDesc());

        getFilePath(entity, validator);

        sceneryMapper.updateByPrimaryKeySelective(entity);
    }

    /**
     * 上传文件-设置文件路径
     */
    private void getFilePath(SceneryEntity entity, CreateOrUpdateSceneryDTO validator) {
        MultipartFile[] sceneryImgs = validator.getSceneryImg();
        String[] imagePath = new String[sceneryImgs.length];
        if (sceneryImgs.length > 0) {
            for (int i = 0; i < sceneryImgs.length; i++) {
                MultipartFile sceneryImg = sceneryImgs[i];
                String imgname = sceneryImg.getOriginalFilename();
                try (InputStream inputStream = sceneryImg.getInputStream()) {
                    imagePath[i] = ioUtil.uploadFile(inputStream, imgname);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        MultipartFile sceneryVideo = validator.getSceneryVideo();
        String videoPath = null;
        if (sceneryVideo != null) {
            String videoName = sceneryVideo.getOriginalFilename();
            videoPath = null;
            try (InputStream inputStream = sceneryVideo.getInputStream()) {
                videoPath = ioUtil.uploadFile(inputStream, videoName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        entity.setSceneryImg(JSON.toJSONString(imagePath));
        entity.setSceneryVideo(videoPath);
    }
}
